package com.colin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.colin.mapper.UsersMapper;
import com.colin.pojo.Users;
import com.colin.service.IUserService;
import com.colin.service.ex.PasswordNotMatchException;
import com.colin.service.ex.UserNotFoundException;
import com.colin.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
@Service
public class UsersServiceImpl implements IUserService {
    @Autowired
    UsersMapper usersMapper;

    /**
     * 根据用户ID查询用户信息
     * @param id 用户ID
     * @return 用户信息
     */
    public Users queryUserInfo(Integer id){
        return usersMapper.selectById(id);
    }

    /**
     * 根据用户名查询用户
     * @param username 用户名
     * @return 用户信息
     */
    public Users queryByUserName(String username){
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.eq("user" , username);
        return usersMapper.selectOne(wrapper);
    }

    @Override
    public Map<String, Object> login(String username, String password) {
        Users user = queryByUserName(username);
        if (user == null){
            throw new UserNotFoundException("用户信息不存在");
        }
        if(!user.getPass().equals(password)){
            throw new PasswordNotMatchException("密码不正确");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("user" , user.getUser());
        map.put("username" , user.getName());
        map.put("uid" , user.getId());
        return map;
    }

    /**
     * 插入用户数据
     * @param user  用户名
     * @param pass  密码
     * @return 影响的结果数
     */
    public Integer insertUser(String user , String pass){
        Users users = new Users();
        users.setUser(user);
        users.setPass(pass);
        int insert = usersMapper.insert(users);
        if (insert > 0){
            return users.getId();
        }
        return insert;
    }

    /**
     * 更新用户
     * @param users 用户对象
     * @return 影响的行数
     */
    public Integer updateUser(Users users){
        return usersMapper.updateById(users);
    }

    /**
     * 更新用户密码
     * @param id 用户ID
     * @param password 用户密码
     * @return 影响的行数
     */
    public Integer updateUserPass(Integer id , String password){
        Users users = new Users();
        users.setId(id);
        users.setPass(password);
        return updateUser(users);
    }

    // 获取所有的技能
    public ArrayList<Object> getSkills(){
        ArrayList<Object> skills = new ArrayList<>();
        skills.add("Java");
        skills.add("PHP");
        skills.add("JavaScript");
        skills.add("Python");
        skills.add("Go");
        skills.add("Linux");
        skills.add("Mysql");
        skills.add("SpringMVC");
        return skills;
    }

    // 获取性别
    public ArrayList<Object> getSex(){
        ArrayList<Object> sex = new ArrayList<>();
        sex.add("男");
        sex.add("女");
        return sex;
    }
}
